home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 03 / wintro6 / tick.c < prev   
C/C++ Source or Header  |  1991-05-01  |  11KB  |  345 lines

  1. /*==================================================================*/
  2. /*                                                                  */
  3. /* File    : TICK.C                                                 */
  4. /*                                                                  */
  5. /* Purpose : Routines pertaining to the adding and modification of  */
  6. /*           tickers.                                               */
  7. /*                                                                  */
  8. /* History :                                                        */
  9. /*                                                                  */
  10. /* Written by Marc Adler/Magma Systems for Microsoft Systems Journal*/
  11. /*==================================================================*/
  12.  
  13. #include <windows.h>
  14. #include "stock.h"
  15.  
  16.  
  17. /*******************************************************************/
  18. /*                                                                 */
  19. /* Function : AddTickDlgProc()                                     */
  20. /*                                                                 */
  21. /* Purpose  : Dialog box proc for adding tickers.                  */
  22. /*                                                                 */
  23. /* Returns  :                                                      */
  24. /*                                                                 */
  25. /*******************************************************************/
  26. BOOL FAR PASCAL AddTickDlgProc(hDlg, msg, wParam, lParam)
  27.   HWND hDlg;
  28.   WORD msg;
  29.   WORD wParam;
  30.   LONG lParam;
  31. {
  32.   RECT        r;
  33.   char        buf[80];
  34.   LPSTOCKINFO lpStockInfo;
  35.   LPTICK      lpTick;
  36.   TICK        tick;
  37.   HWND        hWnd;
  38.  
  39.   static int idxTickEdited;
  40.  
  41.   switch (msg)
  42.   {
  43.     case WM_INITDIALOG:
  44.       /*
  45.          Make sure that there is a current stock information record
  46.          which we can append the ticker onto.
  47.       */
  48.       if (!hCurrStockInfo)
  49.         EndDialog(hDlg, FALSE);
  50.  
  51.       if ((idxTickEdited = (int) lParam) != -1)
  52.       {
  53.         /*
  54.           Get a pointer to the stock info record
  55.         */
  56.         lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo);
  57.         if (lpStockInfo == NULL)
  58.         {
  59.           ErrorMessage(ERR_BADLOCK);
  60.           goto byebye;
  61.         }
  62.         if (!(lpTick = (LPTICK) GlobalLock(lpStockInfo->hTicks)))
  63.         {
  64.           ErrorMessage(ERR_BADLOCK);
  65.           GlobalUnlock(hCurrStockInfo);
  66.           goto byebye;
  67.         }
  68.  
  69.         SetDlgItemLong(hDlg, ID_TICK_PRICE, lpTick[idxTickEdited].price);
  70.         SetDlgItemLong(hDlg, ID_TICK_VOLUME, lpTick[idxTickEdited].dwVolume);
  71.  
  72.         GlobalUnlock(lpStockInfo->hTicks);
  73.         GlobalUnlock(hCurrStockInfo);
  74.   
  75.       }
  76.  
  77.       return TRUE;
  78.  
  79.  
  80.     case WM_COMMAND:
  81.       switch (wParam)
  82.       {
  83.         /*
  84.           The user chose the OK button...
  85.         */
  86.         case IDOK:
  87.           /*
  88.             Get a pointer to the stock info record
  89.           */
  90.           lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo);
  91.           if (lpStockInfo == NULL)
  92.           {
  93.             ErrorMessage(ERR_BADLOCK);
  94.             goto byebye;
  95.           }
  96.  
  97.           /*
  98.             Are we entering the first ticker? If so, then allocate a
  99.             ticker array for the stock. We allocate 64 tickers
  100.             initially.
  101.           */
  102.           if (lpStockInfo->hTicks == NULL)
  103.           {
  104.             lpStockInfo->StockFile.nTicks = 0;
  105.             lpStockInfo->nTicksAllocated  = TICKER_CHUNK;
  106.             lpStockInfo->hTicks = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,
  107.                                        (DWORD) sizeof(TICK) * TICKER_CHUNK);
  108.             if (lpStockInfo->hTicks == NULL)
  109.             {
  110. tickerr:
  111.               ErrorMessage(ERR_CANTALLOCTICKER);
  112.               GlobalUnlock(hCurrStockInfo);
  113.               goto byebye;
  114.             }
  115.           }
  116.  
  117.           /*
  118.             Make sure that we do not overflow the ticker array. If
  119.             there is a chance of this, then reallocate.
  120.           */
  121.           if (idxTickEdited == -1 && 
  122.               lpStockInfo->StockFile.nTicks + 1 >= 
  123.                                      lpStockInfo->nTicksAllocated)
  124.           {
  125.             HANDLE h;
  126.             lpStockInfo->nTicksAllocated *= 2;
  127.             h = GlobalReAlloc(lpStockInfo->hTicks,
  128.                               (DWORD) sizeof(TICK) *
  129.                                       lpStockInfo->nTicksAllocated,
  130.                               GMEM_MOVEABLE);
  131.             if (h == NULL)
  132.               goto tickerr;
  133.             lpStockInfo->hTicks = h;
  134.           }
  135.  
  136.           /*
  137.             Get a pointer to the ticker array
  138.           */
  139.           if (!(lpTick = (LPTICK) GlobalLock(lpStockInfo->hTicks)))
  140.           {
  141.             ErrorMessage(ERR_BADLOCK);
  142.             GlobalUnlock(hCurrStockInfo);
  143.             goto byebye;
  144.           }
  145.  
  146.           /*
  147.             Get the price and volume and out it in the last element
  148.             of the ticker array.
  149.             KLUDGE NOTES :
  150.               a) We really should sort the records by date...
  151.               b) We ignore the date here.
  152.           */
  153.           tick.price = GetDlgItemLong(hDlg, ID_TICK_PRICE, 
  154.                                       NULL, FALSE);
  155.           tick.dwVolume = GetDlgItemLong(hDlg, ID_TICK_VOLUME,
  156.                                       NULL, FALSE);
  157.  
  158.           /*
  159.             Copy the ticker structure
  160.           */
  161.           if (idxTickEdited == -1)
  162.             lpTick[lpStockInfo->StockFile.nTicks++] = tick;
  163.           else
  164.             lpTick[idxTickEdited] = tick;
  165.           lpStockInfo->dwFlags |= STATE_DIRTY;
  166.  
  167.           /*
  168.             Unlock the memory and get outta here...
  169.           */
  170.           GlobalUnlock(lpStockInfo->hTicks);
  171.           GlobalUnlock(hCurrStockInfo);
  172.           EndDialog(hDlg, TRUE);
  173.           break;
  174.  
  175.  
  176.         /*
  177.           The user chose the CANCEL button....
  178.         */
  179.         case IDCANCEL :
  180. byebye:
  181.           EndDialog(hDlg, FALSE);
  182.           break;
  183.       }
  184.       return TRUE;
  185.  
  186.     default:
  187.       return FALSE;
  188.   }
  189. }
  190.  
  191.  
  192. /*******************************************************************/
  193. /*                                                                 */
  194. /* Function : ChangeTickDlgProc()                                  */
  195. /*                                                                 */
  196. /* Purpose  : Dialog box proc for editing tickers.                 */
  197. /*                                                                 */
  198. /* Returns  :                                                      */
  199. /*                                                                 */
  200. /*******************************************************************/
  201. BOOL FAR PASCAL ChangeTickDlgProc(hDlg, msg, wParam, lParam)
  202.   HWND hDlg;
  203.   WORD msg;
  204.   WORD wParam;
  205.   LONG lParam;
  206. {
  207.   HWND        hLB;
  208.   int         i, rc;
  209.   char        buf[80];
  210.   LPSTOCKINFO lpStockInfo;
  211.   LPTICK      lpTick;
  212.   TICK        tick;
  213.   FARPROC     lpfn;
  214.  
  215.   switch (msg)
  216.   {
  217.     case WM_INITDIALOG:
  218.       /*
  219.          Make sure that there is a current stock information record
  220.          which we can append the ticker onto.
  221.       */
  222.       if (!hCurrStockInfo)
  223.         EndDialog(hDlg, FALSE);
  224.  
  225.       lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo);
  226.       if (lpStockInfo == NULL)
  227.       {
  228.         ErrorMessage(ERR_BADLOCK);
  229.         goto byebye;
  230.       }
  231.       /*
  232.         Get a pointer to the ticker array
  233.       */
  234.       if (!(lpTick = (LPTICK) GlobalLock(lpStockInfo->hTicks)))
  235.       {
  236.         ErrorMessage(ERR_BADLOCK);
  237.         GlobalUnlock(hCurrStockInfo);
  238.         goto byebye;
  239.       }
  240.  
  241.       hLB = GetDlgItem(hDlg, ID_LISTBOX);
  242.       SendMessage(hLB, WM_SETREDRAW, FALSE, 0L);
  243.  
  244.       for (i = 0;  i < lpStockInfo->StockFile.nTicks;  i++)
  245.       {
  246.         sprintf(buf, "%d\t%ld\t%ld", i, lpTick[i].price, lpTick[i].dwVolume);
  247.         SendMessage(hLB, LB_ADDSTRING, -1, (DWORD) (LPSTR) buf);
  248.       }
  249.  
  250.       SendMessage(hLB, WM_SETREDRAW, TRUE,  0L);
  251.  
  252.       GlobalUnlock(lpStockInfo->hTicks);
  253.       GlobalUnlock(hCurrStockInfo);
  254.       return TRUE;
  255.  
  256.  
  257.     case WM_COMMAND:
  258.       switch (wParam)
  259.       {
  260.         /*
  261.           The user chose the OK button...
  262.         */
  263.         case ID_LISTBOX :
  264.           if (HIWORD(lParam) != LBN_DBLCLK)
  265.             return TRUE;
  266.           /*
  267.             else fall through ...
  268.           */
  269.  
  270.         case IDOK:
  271.           lpStockInfo = (LPSTOCKINFO) GlobalLock(hCurrStockInfo);
  272.           if (lpStockInfo == NULL)
  273.           {
  274.             ErrorMessage(ERR_BADLOCK);
  275.             goto byebye;
  276.           }
  277.           /*
  278.             Get a pointer to the ticker array
  279.           */
  280.           if (!(lpTick = (LPTICK) GlobalLock(lpStockInfo->hTicks)))
  281.           {
  282.             ErrorMessage(ERR_BADLOCK);
  283.             GlobalUnlock(hCurrStockInfo);
  284.             goto byebye;
  285.           }
  286.  
  287.           hLB = GetDlgItem(hDlg, ID_LISTBOX);
  288.           i = (int) SendMessage(hLB, LB_GETCURSEL, 0, 0L);
  289.           if (i == LB_ERR)
  290.             break;
  291.  
  292.           /*
  293.             'i' is the index of the entry we want to edit...
  294.           */
  295.           lpfn = MakeProcInstance(AddTickDlgProc, hThisInstance);
  296.           rc = DialogBoxParam(hThisInstance, "AddTick", hDlg, lpfn, (LONG) i);
  297.           FreeProcInstance(lpfn);
  298.  
  299.           if (rc == TRUE)
  300.           {
  301.             SendMessage(hLB, LB_DELETESTRING, i, 0L);
  302.             sprintf(buf, "%d\t%ld\t%ld", i, lpTick[i].price, lpTick[i].dwVolume);
  303.             SendMessage(hLB, LB_INSERTSTRING, i, (DWORD) (LPSTR) buf);
  304.             SendMessage(hLB, LB_SETCURSEL, i, 0L);
  305.           }
  306.  
  307.           GlobalUnlock(lpStockInfo->hTicks);
  308.           GlobalUnlock(hCurrStockInfo);
  309.           break;
  310.  
  311.         /*
  312.           The user chose the CANCEL button....
  313.         */
  314.         case IDCANCEL :
  315. byebye:
  316.           EndDialog(hDlg, FALSE);
  317.           break;
  318.       }
  319.       return TRUE;
  320.  
  321.     default:
  322.       return FALSE;
  323.   }
  324. }
  325.  
  326.  
  327. LONG FAR PASCAL GetDlgItemLong(HWND hDlg, WORD id, BOOL FAR *lpTranslated,
  328.                     BOOL bSigned)
  329. {
  330.   extern long atol();
  331.   char szBuf[64];
  332.  
  333.   GetDlgItemText(hDlg, id, (LPSTR) szBuf, sizeof(szBuf));
  334.   return atol(szBuf);
  335. }
  336.  
  337. VOID FAR PASCAL SetDlgItemLong(HWND hDlg, WORD id, LONG lNum)
  338. {
  339.   char szBuf[64];
  340.  
  341.   sprintf(szBuf, "%ld", lNum);
  342.   SetDlgItemText(hDlg, id, (LPSTR) szBuf);
  343. }
  344.  
  345.